package com.epfresh.supplier.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.DebtEntity;

import java.util.List;

/**
 * Created by shenjie on 2017/9/21.
 */

public class DebtAdapter extends BaseAdapter {
    private Context context;
    private List<DebtEntity.DebtListEntity> list;
    private ImageClickListener listener;

    public List<DebtEntity.DebtListEntity> getList() {
        return list;
    }

    public void setList(List<DebtEntity.DebtListEntity> list) {
        this.list = list;
    }

    public DebtAdapter(Context context, List<DebtEntity.DebtListEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.list_debt,null);
            convertView.setTag(holder);
            holder.select= (ImageView) convertView.findViewById(R.id.select);
            holder.menuId= (TextView) convertView.findViewById(R.id.menuId);
            holder.custominfo= (TextView) convertView.findViewById(R.id.customerinfo);
            holder.time= (TextView) convertView.findViewById(R.id.time);
            holder.wait= (TextView) convertView.findViewById(R.id.wait);
            holder.debt= (TextView) convertView.findViewById(R.id.debt);
        }
        holder= (ViewHolder) convertView.getTag();
        holder.select.setTag(position);
        holder.select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p= (int) v.getTag();
                list.get(p).setSelect(!list.get(p).isSelect());
                notifyDataSetChanged();
                if(listener!=null){
                    listener.slectClick();
                }
            }
        });
        DebtEntity.DebtListEntity entity=list.get(position);
        if(entity.isSelect()){
            holder.select.setImageResource(R.mipmap.select);
        }else {
            holder.select.setImageResource(R.mipmap.unselect_small);
        }
        holder.menuId.setText(" "+entity.getOrderNo());
        holder.custominfo.setText(entity.getCustomerName()+" "+entity.getBuyerPhone());
        holder.time.setText(entity.getCreateOrderTimeStr());
        holder.wait.setText("应收: "+entity.getReceivableFee()+"元");
        holder.debt.setText("欠款: "+entity.getDebtFee()+"元");

        return convertView;
    }

    class ViewHolder{
        ImageView select;
        TextView menuId,custominfo,time,wait,debt;
    }

    public interface ImageClickListener{
        public void slectClick();
    }

    public ImageClickListener getListener() {
        return listener;
    }

    public void setListener(ImageClickListener listener) {
        this.listener = listener;
    }
}
